package com.aishua.fire.aspect;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.aishua.fire.common.JsonFormatUtil;
import com.aishua.fire.common.eenum.LogBlackRespEnum;
import com.google.common.base.Stopwatch;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@Aspect
public class ControllerAspect {
	
    @Around("execution(public * com.aishua.fire.controller..*.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        Stopwatch stopwatch = Stopwatch.createStarted();

        //获取请求对象request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        
//        log.info("执行Controller开始: " + pjp.getSignature() + " 参数：" + Lists.newArrayList(pjp.getArgs()).toString());
        Object resultEntity = pjp.proceed(pjp.getArgs());
        log.info("耗时：" + stopwatch.stop().elapsed(TimeUnit.MILLISECONDS) + "(毫秒).");
        if(resultEntity == null) {
//        	ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        	HttpServletResponse response = attributes.getResponse();
//        	Map<String,String> map = new HashMap<String,String>();
//        	map.put("status","200");
//        	String voidReturn = JSON.toJSONString(map);
//        	response.getWriter().write(voidReturn);
        	if(!LogBlackRespEnum.isInBlackList(request)) {
        		log.info("----------<-------method+uri返回信息:{}+{},body返回数据信息为:\n{}",request.getMethod(),request.getRequestURI());
        	}
        	return null;
        }
         
        if(!LogBlackRespEnum.isInBlackList(request)) {
        	log.info("----------<-------method+uri返回信息:{}+{},body返回数据信息为:\n{}",request.getMethod(),request.getRequestURI());
        }
        return resultEntity;
    }

}